using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library.Components.Security;
using Framework.Library.Components.Commands;
using Framework.Library;
using Framework.Library.Utilities.Database;
using Framework.Library.Components.Database;
using Framework.Library.Utilities;

namespace Framework.Portal.Administrator.Database
{
    public partial class ViewsProperty : AuthenticatedControl
    {
        _View objView = new _View();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
            if (!IsPostBack)
            {
                dropViews.DataSource = objView.GetViews();
                dropViews.DataTextField = "completename";
                dropViews.DataValueField = "id";
                dropViews.DataBind();

                ListItem myItem = dropViews.Items.FindByValue(Request.QueryString["id"].ToString());
                if (myItem != null)
                    myItem.Selected = true;

                objView.ID = Convert.ToInt64(dropViews.SelectedValue);
                objView.LoadProperties();

                //load properties
                loadProperties(objView);

                lblCommandName.Text = AppEnv.CommandName();
            }
        }

        protected void dropViews_SelectedIndexChanged(object sender, EventArgs e)
        {
            objView.ID = Convert.ToInt64(dropViews.SelectedValue);
            objView.LoadProperties();
            loadProperties(objView);
        }

        private void loadProperties(_View objView)
        {
            loadColumns(objView);
            loadDefinition(objView);
            loadDepencecies(objView);

            //Navigation Urls
            lnkViewsList.NavigateUrl = AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.View));
            lnkViewDrop.NavigateUrl = AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.ViewProperty)) + "&action=drop&id=" + objView.ID;
        }

        private void loadColumns(_View myView)
        {
            gridColumns.DataSource = myView.GetColumns();
            gridColumns.DataBind();
        }

        private void loadDefinition(_View myView)
        {
            txtDefinition.Text = "";
            objView.SetDefinition();
            while (myView.Definition.Read())
            {
                txtDefinition.Text += myView.Definition.GetString(0).ToString();
            }
        }

        private void loadDepencecies(_View myView)
        {
            //Clear treeview
            treeDependencies.FindNode("Ref/V").ChildNodes.Clear();
            treeDependencies.FindNode("Ref/P").ChildNodes.Clear();
            treeDependencies.FindNode("Refd/U").ChildNodes.Clear();
            treeDependencies.FindNode("Refd/V").ChildNodes.Clear();
            treeDependencies.FindNode("Refd/P").ChildNodes.Clear();
            //treeDependencies.FindNode("Refd/FN").ChildNodes.Clear();
            //******************************
            foreach (DataRow objRow in myView.GetReferences().Rows)
            {
                TreeNode myNode = new TreeNode();
                switch (objRow["xtype"].ToString().Trim())
                {
                    case "U":
                        //table
                        myNode.Text = objRow["completeName"].ToString();
                        myNode.NavigateUrl = "~/modules/Databases/tables/table_properties.aspx?id=" + objRow["id"].ToString() + "&dbname=" + myView.Name;
                        treeDependencies.FindNode(objRow["type"].ToString() + "/U").ChildNodes.Add(myNode);
                        break;
                    case "V":
                        //view
                        myNode.Text = objRow["completeName"].ToString();
                        myNode.NavigateUrl = "view_properties.aspx?id=" + objRow["id"].ToString() + "&dbname=" + myView.Name;
                        treeDependencies.FindNode(objRow["type"].ToString() + "/V").ChildNodes.Add(myNode);
                        break;
                    case "P":
                        //procedure
                        myNode.Text = objRow["completeName"].ToString();
                        myNode.NavigateUrl = "~/modules/Databases/storedprocedure/proc_properties.aspx?id=" + objRow["id"].ToString() + "&dbname=" + myView.Name;
                        treeDependencies.FindNode(objRow["type"].ToString() + "/P").ChildNodes.Add(myNode);
                        break;
                    case "FN":
                        //scalar function
                        myNode.Text = objRow["name"].ToString();
                        myNode.NavigateUrl = "~/modules/Databases/functions/func_properties.aspx?id=" + objRow["id"].ToString() + "&dbname=" + myView.Name;
                        treeDependencies.FindNode(objRow["type"].ToString() + "/FN").ChildNodes.Add(myNode);
                        break;

                }
            }
        }

        protected void gridColumns_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DataRowView rowView = (DataRowView)e.Row.DataItem;
            if (rowView != null)
            {
                Label myLabel = (Label)e.Row.FindControl("lblNull");

                switch (rowView["isnullable"].ToString())
                {
                    case "1":
                        myLabel.Text = "True";
                        break;
                    case "0":
                        myLabel.Text = "False";
                        break;
                }

            }
        }

        protected void btnUpdateDef_Click(object sender, EventArgs e)
        {            
            objView.ID = Convert.ToInt64(dropViews.SelectedValue);
            objView.LoadProperties();
            try
            {
                //if backup needed create a copy
                if (checkBackup.Checked)
                {
                    //rename current view
                    objView.Rename();
                }
                else
                {
                    //drop view
                    objView.Drop();
                }
                //execute definition
                objView.ExecuteQuery(txtDefinition.Text);
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
                lblError.Text = "<p>" + ex.Message + "</p>";
            }
        }
    }
}